var service = require('../../utilService.js')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    isHome: {
      type: Number,
      value: 0
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    users: [],
    isShow: false
  },
 
  /**
   * 组件的方法列表
   */
  methods: {
    ready: function () {
      var that = this
      var arg = arguments;
      service.possilbeFriendsRequest(function (data) {
        var isShow = true;
        if (data.length == 0){
          isShow = false;
        };
        that.setData({ users: data, isShow: isShow })
        if (arg.length > 0) {
          setTimeout(function () {
            return typeof arg[0]() == 'funnction'
          }, 2000);
        }
      },
        function (error) {

        })
    },
    close() {
      this.setData({ isShow: false })
      this.triggerEvent("possibleFriendsClose")
    },
    check() {
      this.triggerEvent("possibleFriendsCheck")
    },
    checkDimiss() {
      var count = 0
      for (var index in this.data.users) {
        if (this.data.users[index].attention == null || this.data.users[index].attention == false) {
          count += 1
        }
      }
      this.setData({isShow: (count > 0 ? true : false)})
    },
    add(e) {
      var index = parseInt(e.currentTarget.id)
      var user = e.currentTarget.dataset.item
      var that = this
      if (user.attention == true){
        wx.navigateTo({
          url: '/mineSubs/pages/homepage/personalhomepage?userId=' + user.id
        })
        return
      }
      service.addPossibleFriends(user.id, function (data) {
        var currentUser = that.properties.users[index]
        currentUser.attention = true
        var array = that.data.users
        array.splice(index, 1, currentUser)
        that.setData({ users: array })
        console.log(that.data.users)
          wx.showToast({
            title: '关注成功',
            icon: 'none',
            duration: 2000,
            complete: function() {
              // that.checkDimiss()
            }
          })
      },
        function (error) {
          wx.showToast({
            title: '关注失败',
            icon: 'none',
            duration: 2000,
          })
        })
      this.triggerEvent("possibleFriendsAdd", user)
    }
  }
})
